package cc.rico.tutorial.sync;

import org.I0Itec.zkclient.ZkClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by rico on 2018/8/19.
 */
public class ConfigManager {
    private static final Logger logger = LoggerFactory.getLogger(ConfigManager.class);
    public static final String CONFIG_PATH = "/ftpConfig";
    private FtpConfig ftpConfig;

    public void load() {
        logger.info("##ConfigManager.load...");
        ftpConfig = new FtpConfig("192.168.1.1", "21", "test", "123456");
    }

    public void sync() {
        ZkClient zkClient = new ZkClient("192.168.223.140:2181");

        if(!zkClient.exists(CONFIG_PATH)) {
            zkClient.createPersistent(CONFIG_PATH, true);
        }

        zkClient.writeData(CONFIG_PATH, ftpConfig);
        zkClient.close();
    }

    public void update() {
        logger.info("##.ConfigManager.update...");
        ftpConfig = new FtpConfig("192.168.1.2", "22", "test", "123456");
    }
}
